package Gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
/**
 * this class implements gui of lend histogram
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */



public class LendHistogram extends ApplicationFrame {

   

	private static final long serialVersionUID = 1L;

    public LendHistogram(String title, int avdw , int midw, int avgnorm, int midnorm) {
    	   super(title);
    	   CategoryDataset dataset = createDataset(avdw ,midw,avgnorm,midnorm); 
    	   JFreeChart chart = createChart(dataset, title);
    	   ChartPanel chartPanel = new ChartPanel(chart, false); 
    	   chartPanel.setPreferredSize(new Dimension(400, 300));
    	   
    	   setContentPane(chartPanel);	
    	   
    }

    public void windowClosing(final WindowEvent evt)
    {
    	if(evt.getWindow() == this){
    	dispose();
    	}
    }

	/**
     * Returns a sample dataset.
	 * @param active 
	 * @param late 
	 * @param lend 
	 * @param freeze 
     * 
     * @return The dataset.
     */
    private CategoryDataset createDataset(int active, int lock, int freeze, int lend) {
        
        // row keys...
        String series1 = "Avg Wanted";
        String series2 = "Mid Wanted";
        String series3 = "Avg Not Wanted";
        String series4 = "Mid Not Wanted";
       // String series5 = "Books that didn't returned in time ";
       // String series3 = "Third";
 
        // column keys...
        String category1 = "Avg Wanted";
        String category2 = "Mid Wanted";
        String category3 = "Avg Not Wanted";
        String category4 = "Mid Not Wanted";
       // String category5 = "Late in Return";

       // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 
         dataset.addValue(active, series1, category1);
         dataset.addValue(lock, series2, category2);
         dataset.addValue(freeze, series3, category3);
         dataset.addValue(lend, series4, category4);
        

        
       return dataset;     
     }

    
    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * @param title 
     * 
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset, String title) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Lend Report",         // chart title
            "Status",               // domain axis label
            "Number Of Days",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);

        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    
    
    /**
     * This methode closes the histogram .
    
     *
     */
    public void windowClosing1(final WindowEvent evt){
    	if(evt.getWindow() == this){
    	dispose();}}
}